package com.wowoai.common.page;

public class BasePage implements Paginable {
    public static final int DEF_COUNT = 20;
    protected int totalCount = 0;
    protected int pageSize = 20;
    protected int pageNum = 1;

    public BasePage() {
    }

    public BasePage(int pageNum, int pageSize, int totalCount) {
        this.setTotalCount(totalCount);
        this.setPageSize(pageSize);
        this.setPageNum(pageNum);
        this.adjustPageNum();
    }

    private void adjustPageNum() {
        if (this.pageNum != 1) {
            int tp = this.getTotalPage();
            if (this.pageNum > tp) {
                this.pageNum = tp;
            }

        }
    }

    /** @deprecated */
    @Deprecated
    public int getPageNo() {
        return this.pageNum;
    }

    public int getPageNum() {
        return this.pageNum;
    }

    public int getPageSize() {
        return this.pageSize;
    }

    public int getTotalCount() {
        return this.totalCount;
    }

    public int getTotalPage() {
        int totalPage = this.totalCount / this.pageSize;
        if (totalPage == 0 || this.totalCount % this.pageSize != 0) {
            ++totalPage;
        }

        return totalPage;
    }

    public boolean isFirstPage() {
        return this.pageNum <= 1;
    }

    public boolean isLastPage() {
        return this.pageNum >= this.getTotalPage();
    }

    public int getNextPage() {
        return this.isLastPage() ? this.pageNum : this.pageNum + 1;
    }

    public int getPrePage() {
        return this.isFirstPage() ? this.pageNum : this.pageNum - 1;
    }

    public void setTotalCount(int totalCount) {
        if (totalCount < 0) {
            this.totalCount = 0;
        } else {
            this.totalCount = totalCount;
        }

    }

    public void setPageSize(int pageSize) {
        if (pageSize < 1) {
            this.pageSize = 20;
        } else {
            this.pageSize = pageSize;
        }

    }

    /** @deprecated */
    @Deprecated
    public void setPageNo(int pageNo) {
        if (pageNo < 1) {
            this.pageNum = 1;
        } else {
            this.pageNum = pageNo;
        }

    }

    public void setPageNum(int pageNum) {
        if (pageNum < 1) {
            this.pageNum = 1;
        } else {
            this.pageNum = pageNum;
        }

    }
}

